package com.azusa.xbjy.service.impl;

import com.azusa.xbjy.dao.ArticleDao;
import com.azusa.xbjy.dao.ArticleMapper;
import com.azusa.xbjy.dao.UserMapper;
import com.azusa.xbjy.entity.Article;
import com.azusa.xbjy.entity.User;
import com.azusa.xbjy.service.ArticleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleDao articleDao;

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public PageInfo<Article> findAllPage(int pageNum, int pageSize, String title) {
        PageHelper.startPage(pageNum, pageSize);
        return new PageInfo<Article>(articleMapper.findByTitle(title));
    }

    @Override
    public void save(Article article) {
        articleDao.save(article);
    }

    @Override
    public Map<String,Object> findById(int aid,int userId) {
        Article article = articleDao.findById(aid).get();
        //设置收藏数
        article.setCount(articleMapper.findArticleCount(aid));
        //查询登录用户收藏的文章id集合
        List<Integer> collects = articleMapper.findUserCollect(userId);
        //是否有被登录用户收藏
        if (collects.contains(aid)){
            article.setCollect(true);
        }
        HashMap<String, Object> resultMap = new HashMap<>();

        //查询登录用户所关注的用户id集合
        List<Integer> focusedList = userMapper.findFocusedByUserId(userId);
        //查询收藏该文章的用户id集合
        List<Integer> userList = articleMapper.findArticleCollect(aid);
        //focusedList和userList的交集
        focusedList.retainAll(userList);
        //登录用户关注了该文章的关注用户集合
        List<User> users = new ArrayList<>();
        if (focusedList.size()>0){
            users = userMapper.findMyUsers(focusedList);
            //清除密码
            for (User user : users) {
                user.setPassword(null);
                user.setFocused(true);
            }
        }
        resultMap.put("users",users);
        resultMap.put("article",article);
        return resultMap;
    }

    @Override
    public void updateBrowseCount(int aid) {
        articleMapper.updateBrowseCount(aid);
    }

    @Override
    public PageInfo<Article> findCollectPage(int pageNum, int pageSize, int uid, String title){
        PageHelper.startPage(pageNum, pageSize);
        //查询登录用户收藏的文章id集合
        List<Integer> aIds = articleMapper.findUserCollect(uid);
        //查询文章集合
        List<Article> collect = articleMapper.selectCollect(aIds, title);
        return new PageInfo<Article>(collect);
    }

    @Override
    public void updateCollect(int aid, int uid,boolean collect) {
        if (collect){
            //收藏文章
            userMapper.insertCollect(aid,uid);
        }else{
            //取消收藏文章
            userMapper.deleteCollect(aid,uid);
        }
    }
}
